package com.cardbaobao.cardbabyclient.widget;

import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.cardbaobao.cardbabyclient.R;


/**
 * Description:
 * Created by FHW
 * on 2017/06/27
 */

public class BottomPopupOption implements View.OnClickListener {

    //上下文对象
    private Context mContext;
    //PopupWindow对象
    private PopupWindow mPopupWindow;
    //点击事件
    private onPopupWindowItemClickListener itemClickListener;

    /**
     * 一个参数的构造方法，用于弹出无标题的PopupWindow
     *
     * @param context
     */
    public BottomPopupOption(Context context) {
        this.mContext = context;
    }


    /**
     * 设置选项的点击事件
     *
     * @param itemClickListener
     */
    public void setItemClickListener(onPopupWindowItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }


    /**
     * 弹出PopupWindow
     */
    public void showPopupWindow() {
        View popupWindow_view = LayoutInflater.from(mContext).inflate(R.layout.pop_share_bottom, null);

        LinearLayout id_ll_weixin = (LinearLayout) popupWindow_view.findViewById(R.id.id_ll_weixin);
        LinearLayout id_ll_weixin_friends = (LinearLayout) popupWindow_view.findViewById(R.id.id_ll_weixin_friends);
        LinearLayout id_ll_weibo = (LinearLayout) popupWindow_view.findViewById(R.id.id_ll_weibo);
        LinearLayout ll_qq = (LinearLayout) popupWindow_view.findViewById(R.id.ll_qq);

        id_ll_weixin.setOnClickListener(this);
        id_ll_weixin_friends.setOnClickListener(this);
        id_ll_weibo.setOnClickListener(this);
        ll_qq.setOnClickListener(this);

        mPopupWindow = new PopupWindow(popupWindow_view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setAnimationStyle(R.style.popwindow_anim_style);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setWindowAlpha(false);
            }
        });
        show(popupWindow_view);

    }

    public void setOnDismissListener(PopupWindow.OnDismissListener onDismissListener) {
        mPopupWindow.setOnDismissListener(onDismissListener);
    }


    /**
     * 显示PopupWindow
     */
    private void show(View v) {
        if (mPopupWindow != null && !mPopupWindow.isShowing()) {
            mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
        }
        setWindowAlpha(true);
    }


    /**
     * 消失PopupWindow
     */
    public void dismiss() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }

    /**
     * 动态设置Activity背景透明度
     *
     * @param open
     */
    public void setWindowAlpha(boolean open) {
        if (Build.VERSION.SDK_INT < 11) {
            return;
        }
        final Window window = ((Activity) mContext).getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();
        window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        ValueAnimator animator;
        if (open) {
            animator = ValueAnimator.ofFloat(1.0f, 0.5f);
        } else {
            animator = ValueAnimator.ofFloat(0.5f, 1.0f);
        }
        animator.setDuration(400);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float alpha = (float) animation.getAnimatedValue();
                lp.alpha = alpha;
                window.setAttributes(lp);
            }
        });
        animator.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_ll_weixin:
                itemClickListener.onItemClick(0);
                break;
            case R.id.id_ll_weixin_friends:
                itemClickListener.onItemClick(1);
                break;
            case R.id.id_ll_weibo:
                itemClickListener.onItemClick(2);
                break;
            case R.id.ll_qq:
                itemClickListener.onItemClick(3);
                break;
            default:
                itemClickListener.onItemClick(-1);
                break;
        }
    }


    /**
     * 点击事件选择回调
     */
    public interface onPopupWindowItemClickListener {
        void onItemClick(int position);
    }

    public interface onClickAllListener {
        void onClickAll();
    }

}
